<?php

namespace App\Http\Livewire\Actions;

use App\Models\Archive;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;

class Thumb extends Component
{
    public $count;
    public $archive_id;
    public $is_liked;

    public function toggle()
    {
        $archive = Archive::find($this->archive_id);
        if (!Auth::check()) {
            $this->emit('showMessage', [
                'title' => null,
                'content' => '请登录以后再试',
                'type' => 'info',
                'timeout' => 3000,
                'revoke' => false,
            ]);
            return;
        }
        $user = Auth::user();
        $user->toggleLike($archive);
        if ($user->hasLiked($archive)) {
            $this->count++;
            $this->is_liked = true;
            $this->emit('showMessage', [
               'title' => null,
                'content' => '已加入我喜欢的列表',
                'type' => 'info',
                'timeout' => 3000,
                'revoke' => false,
            ]);
        } else {
            $this->is_liked = false;
            $this->count--;
            $this->emit('showMessage', [
                'title' => null,
                'content' => '已从我喜欢的列表移除',
                'type' => 'info',
                'timeout' => 3000,
                'revoke' => false,
            ]);
        }
    }
    public function render()
    {
        return view('livewire.actions.thumb');
    }
}
